//
//  UrlUtil.m
//  YuanXinCamera
//
//  Created by GJS on 2017/6/12.
//  Copyright © 2017年 GJS. All rights reserved.
//

#import "UrlUtil.h"

@implementation UrlUtil

/**
 * http://www.skyfox.org/ios-url-urlencode-and-urldecode.html
 *
 URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
 URLHostAllowedCharacterSet      "#%/<>?@\^`{|}
 URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}
 URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}
 URLQueryAllowedCharacterSet     "#%<>[\]^`{|}
 URLUserAllowedCharacterSet      "#%/:<>?@[\]^`
 */
//URLEncode
+(NSString*)encodeString:(NSString*)unencodedString {
#ifndef __IPHONE_9_0
    // CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
    // CharactersToLeaveUnescaped = @"[].";
    
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));
#else
    //encode
    NSString *encodedString = [unencodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    /* 当然系统内置的CharacterSet要是满足不了你的话，你可以自定义NSCharacterSet
     NSCharacterSet *c = [NSCharacterSet characterSetWithCharactersInString:@"'();:@&=+$,/?%#[]"];
     encodedString = [unencodedString stringByAddingPercentEncodingWithAllowedCharacters:c];
     */
#endif
    
    return encodedString;
}

//URLDEcode
+(NSString *)decodeString:(NSString*)encodedString {
#ifndef __IPHONE_9_0
    //NSString *decodedString = [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
    
    NSString *decodedString  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                                                                                     (__bridge CFStringRef)encodedString,
                                                                                                                     CFSTR(""),
                                                                                                                     CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
#else
    //decode
    NSString *decodedString = [encodedString stringByRemovingPercentEncoding];
#endif
    return decodedString;
}

//URLEncode
+(NSString*)encodeUrlString:(NSString*)unencodedString {
    NSString * regexPattern = @"[\\s+\u4e00-\u9fa5]"; // 正则匹配空白符和中文 /[\s+\u4e00-\u9fa5]/
    NSRegularExpression * regex = [[NSRegularExpression alloc]initWithPattern:regexPattern options:NSRegularExpressionCaseInsensitive error:nil];
    //NSArray *results = [regex matchesInString:unencodedString options:NSMatchingReportCompletion range:NSMakeRange(0, unencodedString.length)];
    NSTextCheckingResult *result = [regex firstMatchInString:unencodedString options:NSMatchingReportCompletion range:NSMakeRange(0, unencodedString.length)];
    if (result) {
        return [self encodeString:unencodedString];
    }
    return unencodedString;
}

@end
